package com.dm.voicesubtitle.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 应用配置管理类
 */
public class AppConfig {
    private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
    private static AppConfig instance;
    
    private static final String CONFIG_FILE = "config.json";
    
    // 支持的语言列表
    private final Map<String, LanguageConfig> supportedLanguages;
    
    // 音频配置
    private AudioConfig audioConfig;
    
    // 显示配置
    private DisplayConfig displayConfig;
    
    // 语音识别配置
    private SpeechConfig speechConfig;
    
    private AppConfig() {
        this.supportedLanguages = new LinkedHashMap<>();
        initializeDefaultConfig();
    }
    
    public static synchronized AppConfig getInstance() {
        if (instance == null) {
            instance = new AppConfig();
            instance.loadConfig();
        }
        return instance;
    }
    
    /**
     * 初始化默认配置
     */
    private void initializeDefaultConfig() {
        // 初始化支持的语言
        supportedLanguages.put("zh-CN", new LanguageConfig("zh-CN", "中文", "Chinese", "中国語"));
        supportedLanguages.put("en-US", new LanguageConfig("en-US", "英文", "English", "英語"));
        supportedLanguages.put("ja-JP", new LanguageConfig("ja-JP", "日文", "Japanese", "日本語"));
        
        // 默认音频配置
        this.audioConfig = new AudioConfig();
        
        // 默认显示配置
        this.displayConfig = new DisplayConfig();
        
        // 默认语音识别配置
        this.speechConfig = new SpeechConfig();
    }
    
    /**
     * 加载配置文件
     */
    private void loadConfig() {
        File configFile = new File(CONFIG_FILE);
        if (configFile.exists()) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                ConfigData configData = mapper.readValue(configFile, ConfigData.class);
                
                if (configData.audioConfig != null) {
                    this.audioConfig = configData.audioConfig;
                }
                if (configData.displayConfig != null) {
                    this.displayConfig = configData.displayConfig;
                }
                if (configData.speechConfig != null) {
                    this.speechConfig = configData.speechConfig;
                }
                
                logger.info("配置文件加载成功");
            } catch (IOException e) {
                logger.warn("配置文件加载失败，使用默认配置", e);
            }
        } else {
            logger.info("配置文件不存在，使用默认配置");
            saveConfig();
        }
    }
    
    /**
     * 保存配置文件
     */
    public void saveConfig() {
        try {
            ObjectMapper mapper = new ObjectMapper();
            ConfigData configData = new ConfigData();
            configData.audioConfig = this.audioConfig;
            configData.displayConfig = this.displayConfig;
            configData.speechConfig = this.speechConfig;
            
            mapper.writerWithDefaultPrettyPrinter()
                  .writeValue(new File(CONFIG_FILE), configData);
            
            logger.info("配置文件保存成功");
        } catch (IOException e) {
            logger.error("配置文件保存失败", e);
        }
    }
    
    // Getter方法
    public Map<String, LanguageConfig> getSupportedLanguages() {
        return Collections.unmodifiableMap(supportedLanguages);
    }
    
    public AudioConfig getAudioConfig() {
        return audioConfig;
    }
    
    public DisplayConfig getDisplayConfig() {
        return displayConfig;
    }
    
    public SpeechConfig getSpeechConfig() {
        return speechConfig;
    }
    
    /**
     * 语言配置类
     */
    public static class LanguageConfig {
        private String code;
        private String chineseName;
        private String englishName;
        private String japaneseName;
        
        public LanguageConfig() {}
        
        public LanguageConfig(String code, String chineseName, String englishName, String japaneseName) {
            this.code = code;
            this.chineseName = chineseName;
            this.englishName = englishName;
            this.japaneseName = japaneseName;
        }
        
        // Getter和Setter方法
        public String getCode() { return code; }
        public void setCode(String code) { this.code = code; }
        
        public String getChineseName() { return chineseName; }
        public void setChineseName(String chineseName) { this.chineseName = chineseName; }
        
        public String getEnglishName() { return englishName; }
        public void setEnglishName(String englishName) { this.englishName = englishName; }
        
        public String getJapaneseName() { return japaneseName; }
        public void setJapaneseName(String japaneseName) { this.japaneseName = japaneseName; }
    }
    
    /**
     * 音频配置类
     */
    public static class AudioConfig {
        private int sampleRate = 16000;
        private int channels = 1;
        private int bitsPerSample = 16;
        private String audioFormat = "PCM";
        
        public AudioConfig() {}
        
        // Getter和Setter方法
        public int getSampleRate() { return sampleRate; }
        public void setSampleRate(int sampleRate) { this.sampleRate = sampleRate; }
        
        public int getChannels() { return channels; }
        public void setChannels(int channels) { this.channels = channels; }
        
        public int getBitsPerSample() { return bitsPerSample; }
        public void setBitsPerSample(int bitsPerSample) { this.bitsPerSample = bitsPerSample; }
        
        public String getAudioFormat() { return audioFormat; }
        public void setAudioFormat(String audioFormat) { this.audioFormat = audioFormat; }
    }
    
    /**
     * 显示配置类
     */
    public static class DisplayConfig {
        private int windowWidth = 800;
        private int windowHeight = 600;
        private int fontSize = 24;
        private String fontFamily = "微软雅黑";
        private String backgroundColor = "#000000";
        private String textColor = "#FFFFFF";
        private int subtitleDuration = 3000; // 毫秒
        private boolean enableMultiLanguage = true;
        
        public DisplayConfig() {}
        
        // Getter和Setter方法
        public int getWindowWidth() { return windowWidth; }
        public void setWindowWidth(int windowWidth) { this.windowWidth = windowWidth; }
        
        public int getWindowHeight() { return windowHeight; }
        public void setWindowHeight(int windowHeight) { this.windowHeight = windowHeight; }
        
        public int getFontSize() { return fontSize; }
        public void setFontSize(int fontSize) { this.fontSize = fontSize; }
        
        public String getFontFamily() { return fontFamily; }
        public void setFontFamily(String fontFamily) { this.fontFamily = fontFamily; }
        
        public String getBackgroundColor() { return backgroundColor; }
        public void setBackgroundColor(String backgroundColor) { this.backgroundColor = backgroundColor; }
        
        public String getTextColor() { return textColor; }
        public void setTextColor(String textColor) { this.textColor = textColor; }
        
        public int getSubtitleDuration() { return subtitleDuration; }
        public void setSubtitleDuration(int subtitleDuration) { this.subtitleDuration = subtitleDuration; }
        
        public boolean isEnableMultiLanguage() { return enableMultiLanguage; }
        public void setEnableMultiLanguage(boolean enableMultiLanguage) { this.enableMultiLanguage = enableMultiLanguage; }
    }
    
    /**
     * 语音识别配置类
     */
    public static class SpeechConfig {
        private String defaultLanguage = "zh-CN";
        private boolean enableAutoLanguageDetection = true;
        private int recognitionTimeout = 5000; // 毫秒
        private boolean enableInterimResults = true;
        
        public SpeechConfig() {}
        
        // Getter和Setter方法
        public String getDefaultLanguage() { return defaultLanguage; }
        public void setDefaultLanguage(String defaultLanguage) { this.defaultLanguage = defaultLanguage; }
        
        public boolean isEnableAutoLanguageDetection() { return enableAutoLanguageDetection; }
        public void setEnableAutoLanguageDetection(boolean enableAutoLanguageDetection) { 
            this.enableAutoLanguageDetection = enableAutoLanguageDetection; 
        }
        
        public int getRecognitionTimeout() { return recognitionTimeout; }
        public void setRecognitionTimeout(int recognitionTimeout) { this.recognitionTimeout = recognitionTimeout; }
        
        public boolean isEnableInterimResults() { return enableInterimResults; }
        public void setEnableInterimResults(boolean enableInterimResults) { this.enableInterimResults = enableInterimResults; }
    }
    
    /**
     * 配置数据类（用于JSON序列化）
     */
    private static class ConfigData {
        public AudioConfig audioConfig;
        public DisplayConfig displayConfig;
        public SpeechConfig speechConfig;
    }
    
    @Override
    public String toString() {
        return "AppConfig{" +
                "supportedLanguages=" + supportedLanguages.keySet() +
                ", audioConfig=" + audioConfig +
                ", displayConfig=" + displayConfig +
                ", speechConfig=" + speechConfig +
                '}';
    }
} 